/** 
 * ObjectsTree - displays the objects in a tree view
 *
 * @author jprieur
 * @created April 1st 2007
 *
 */
package mygui;

import kernel.*;
import objects.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class ObjectsTree extends JPanel implements kernel.Observer,
						   MouseListener
{
    private JTree _tree;
    private DefaultTreeModel _model;
    private DefaultMutableTreeNode _root;
    private MonIHM _gui;
    
    public static String SINK = "Sinks";
    public static String SENSOR = "Sensor networks";
    public static String BASE_STATION = "Base stations";
    public static String MOBILE_STATION = "Mobile stations";
    public static String OBSTACLE = "Obstacles";

    public ObjectsTree(MonIHM gui)
    {
	super(new BorderLayout());

	_gui = gui;
        _root = new DefaultMutableTreeNode("Objects");
        createNodes(_root);

	_model = new DefaultTreeModel(_root);
	_tree = new JTree(_model);
	_tree.getSelectionModel().setSelectionMode
	    (TreeSelectionModel.SINGLE_TREE_SELECTION);
	//_tree.putClientProperty("JTree.lineStyle", "Horizontal");
	_tree.setCellRenderer(new CellRenderer());
	_tree.addMouseListener(this);

	add("Center", new JScrollPane(_tree));
    }

    protected void createNodes(DefaultMutableTreeNode top) {
	top.removeAllChildren();

	DefaultMutableTreeNode category;
	DefaultMutableTreeNode item;

	Simulator kernel = Simulator.getSimulator();
        Vector<MobileStation> mstations = kernel.getMobileStation();
        Vector<BaseStation> bstations = kernel.getBaseStation();
	Vector<Obstacle> obstacles = kernel.getObstacle();

	if(Simulator.getSimulator().getMode() != Simulator.ADHOC){
	    category = new DefaultMutableTreeNode(BASE_STATION);
	    for(BaseStation base: bstations){
		item = new DefaultMutableTreeNode(base);
		category.add(item);
	    }
	    top.add(category);
	}

	category = new DefaultMutableTreeNode(MOBILE_STATION);
	for(MobileStation mobile: mstations){
	    item = new DefaultMutableTreeNode(mobile);
	    category.add(item);
	}
	top.add(category);

	category = new DefaultMutableTreeNode(OBSTACLE);
	for(Obstacle obstacle: obstacles){
	    item = new DefaultMutableTreeNode(obstacle);
	    category.add(item);
	}
	top.add(category);
    }

    public void update()
    {
	createNodes(_root);
	_model.reload();
    }

    public void mouseClicked(MouseEvent e)
    {
	DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode)_tree.getLastSelectedPathComponent();
	if(dmtn == null) return;
	Object selected = dmtn.getUserObject();
	if(selected == null) return;

	boolean edit = ((DisplayPanel)_gui.getDisplay()).getMode() != DisplayPanel.Mode.SIMULATION;

	if(selected instanceof Sink){
	    Sink m = (Sink)selected;
	    if(e.getClickCount() == 1){
		java.awt.Rectangle rect = new java.awt.Rectangle((int)m.getX(),(int)m.getY(),1,1);
		_gui._display_scroller.getViewport().scrollRectToVisible(rect);
	    }else if(e.getClickCount() == 2){
		if(edit){
		    SinkProperties sprops = new SinkProperties(m);
		}else{
		    _gui.getMonitor().display(m);
		}
	    }
	}else if(selected instanceof MobileStation){
	    MobileStation m = (MobileStation)selected;
	    if(e.getClickCount() == 1){
		java.awt.Rectangle rect = new java.awt.Rectangle((int)m.getX(),(int)m.getY(),1,1);
		_gui._display_scroller.getViewport().scrollRectToVisible(rect);
	    }else if(e.getClickCount() == 2){
		if(edit){
		    MobileProperties mprops = new MobileProperties(m);
		}else{
		    _gui.getMonitor().display(m);
		}
	    }
	}else if(selected instanceof BaseStation){
	    BaseStation m = (BaseStation)selected;
	    if(e.getClickCount() == 1){
		java.awt.Rectangle rect = new java.awt.Rectangle((int)m.getX(),(int)m.getY(),1,1);
		_gui._display_scroller.getViewport().scrollRectToVisible(rect);
	    }else if(e.getClickCount() == 2){
		if(edit){
		    BaseProperties bprops = new BaseProperties(m);
		}else{
		    _gui.getMonitor().display(m);
		}
	    }
	}else if(selected instanceof Obstacle){
	    Obstacle m = (Obstacle)selected;
	    if(e.getClickCount() == 1){
		java.awt.Rectangle rect = new java.awt.Rectangle((int)m.getX(),(int)m.getY(),(int)m.getWidth(),(int)m.getHeight());
		_gui._display_scroller.getViewport().scrollRectToVisible(rect);
	    }else if(e.getClickCount() == 2){
		if(edit){
		    ObstacleProperties oprops = new ObstacleProperties(m);
		}
	    }
	}else if(selected instanceof SensorNetwork){
	    SensorNetwork m = (SensorNetwork)selected;
	    if(e.getClickCount() == 1){
		java.awt.Rectangle rect = new java.awt.Rectangle((int)m.getX(),(int)m.getY(),(int)m.getWidth(),(int)m.getHeight());
		_gui._display_scroller.getViewport().scrollRectToVisible(rect);
	    }else if(e.getClickCount() == 2){
		if(edit){
		    SensorNetworkProperties sprops = new SensorNetworkProperties(m);
		}else{
		    _gui.getMonitor().display(m);
		}
	    }
	}
    }

    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}
    

    
    class CellRenderer extends DefaultTreeCellRenderer {
        private Icon _base, _mobile, _rect, _elli, _sink, _sensor;
	
        public CellRenderer()
	{
	    _base = UIM.createImageIcon("images/base.png","");
	    _mobile = UIM.createImageIcon("images/wireless.png","");
	    _sink = UIM.createImageIcon("images/base.png","");
	    _sensor = UIM.createImageIcon("images/wireless.png","");
	    _rect = UIM.createImageIcon("images/rectangle.png","");
	    _elli = UIM.createImageIcon("images/ellipse.png","");
        }

        public Component getTreeCellRendererComponent(JTree tree,
						      Object value,
						      boolean sel,
						      boolean expanded,
						      boolean leaf,
						      int row,
						      boolean hasFocus) {

            super.getTreeCellRendererComponent(tree, value, sel,
					       expanded, leaf, row,
					       hasFocus);
	    if(leaf){
		setIcon(new ImageIcon());
	    }
	    
	    if(!leaf && value.toString().equals(SINK)){
		setIcon(_sink);
	    }if(!leaf && value.toString().equals(SENSOR)){
		setIcon(_sensor);
	    }if(!leaf && value.toString().equals(MOBILE_STATION)){
		setIcon(_mobile);
	    }if(!leaf && value.toString().equals(BASE_STATION)){
		setIcon(_base);
	    }if(!leaf && value.toString().equals(OBSTACLE)){
		setIcon(_rect);
	    }
            return this;
        }
    }

}